package com.efounder.frame.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import androidx.core.content.res.ResourcesCompat;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.core.xml.StubObject;
import com.efounder.frame.arcmenu.item.ArcMenuSimpleSuperscriptItem;
import com.efounder.frame.tabbar.CircleTextView;
import com.efounder.frame.utils.EFAppAccountUtils;
import com.efounder.frame.xmlparse.EFXmlConstants;
import com.efounder.mobilemanager.R;

/**
 * Created by XinQing on 2016/9/22.
 */

public class EFArcMenuSuperScriptItem extends ArcMenuSimpleSuperscriptItem {

    private ImageView imageView;
    private CircleTextView circleTextView;


    public EFArcMenuSuperScriptItem(Context context, StubObject menuStub) {
        super(context);
        //ImageView
        imageView = new ImageView(context);
        String imagePath = EFAppAccountUtils.getAppAccountImagePath() + "/" + menuStub.getString(EFXmlConstants.ATTR_MENU_ICON,"");
        imageView.setImageDrawable(Drawable.createFromPath(imagePath));
        RelativeLayout.LayoutParams imageViewLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        imageView.setLayoutParams(imageViewLayoutParams);
        this.addView(imageView);

        //CircleTextView
        circleTextView = new CircleTextView(context);
        circleTextView.setBackgroundColor(ResourcesCompat.getColor(context.getResources(),R.color.chat_red,null));
        RelativeLayout.LayoutParams circleTextViewLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        circleTextViewLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        circleTextViewLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        circleTextView.setGravity(Gravity.CENTER);
        circleTextView.setTextColor(ResourcesCompat.getColor(context.getResources(),R.color.ef_white,null));
        circleTextView.setLayoutParams(circleTextViewLayoutParams);
        circleTextView.setTextSize(13);
        circleTextView.setText("0");
        setCircleTextViewVisibility();
        this.addView(circleTextView);
    }

    /**
     * 角标  superscript 加上一个数字(可以是负数，表示减去)
     * @param number
     */
    public void superscriptAddNumber(int number){
        int currentNumber = 0;
        try {
            currentNumber = Integer.parseInt(circleTextView.getText().toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        circleTextView.setText(String.valueOf(currentNumber + number));
        setCircleTextViewVisibility();
        //派发事件
        if (arcMenuSuperscriptItemListener != null){
            arcMenuSuperscriptItemListener.onSuperscriptItemAddNumber(number);
        }

    }

    public int getSuperscriptNumber(){
        int num = 0;
        try {
            num = Integer.parseInt(circleTextView.getText().toString());
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return num;
    }

    public void clearSuperscriptNumber(){
        int num = getSuperscriptNumber();
        superscriptAddNumber(-num);
    }


    private void setCircleTextViewVisibility(){
        try {
            if (Integer.parseInt(circleTextView.getText().toString()) > 0){
                circleTextView.setVisibility(View.VISIBLE);
            }else{
                circleTextView.setVisibility(View.GONE);
            }
        }catch (Exception e){
            e.printStackTrace();
            circleTextView.setVisibility(View.GONE);
        }
    }





}
